/*
 * @Author: zwz
 * @Date: 2023-06-05 15:17:03
 * @LastEditors: zwz
 * @LastEditTime: 2023-06-06 08:44:58
 * @Description: 请填写简介
 */
module.exports = () => {
    return async function authMiddleware(ctx, next) {
      const token = ctx.headers.authorization; // 获取请求头中的授权令牌
      if (!token) {
        ctx.status = 200;
        ctx.body = {
          code:200433,
          msg:'登录过期'
        };
        return;
      }
      try {
        const decoded = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);
        ctx.state.user = decoded; // 将解码后的用户信息保存到上下文中
        await next();
      } catch (err) {
        ctx.status = 200;
        ctx.body = {
          code:200433,
          msg:'登录过期'
        };;
      }
    };
  };